package com.vue.vuedemo.Controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.vue.vuedemo.pojo.Persons;
import com.vue.vuedemo.pojo.Sportscircle;
import com.vue.vuedemo.pojo.Zhuanqu;
import com.vue.vuedemo.service.PersonService;
import com.vue.vuedemo.service.SportsCircleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
@CrossOrigin(origins = {"http://localhost:8080"}) //当前网站向另一个网站请求数据就是跨域
@Controller
public class SportsCircleController {


    @Resource
    SportsCircleService sportsCircleService;

    //查询全部帖子
    @GetMapping(value = "SportfindAll")
    public List<Sportscircle> findAll(){
        List<Sportscircle> list =  sportsCircleService.findAll();
        return list;
    }

    public Integer num;

    //修改帖子喜欢数量 +1
    @GetMapping(value = "SportsUpdateZan")
    public Integer updateZan(Integer sId){
        try {
            sportsCircleService.updateZan(sId);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    //添加帖子
    @GetMapping(value = "SportsAdd")
    public Integer SportsAdd(Integer sPid,String sName,String sContext,String sType,Sportscircle sportscircle, HttpSession session){
        sportscircle.setSName(sName);
        sportscircle.setSContext(sContext);
        sportscircle.setSPId(sPid);
        sportscircle.setsType(sType);
        try {
            sportsCircleService.addSports(sportscircle);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }


    //查看我的帖子
    @GetMapping(value = "pIdSportfindAll")
    public List<Sportscircle> pIdFindAll(Integer pId,HttpSession session){
        List<Sportscircle> list =  sportsCircleService.pIdFindAll(pId);
        return list;
    }

    //删除我的帖子
    @GetMapping(value = "SportsDel")
    public Integer SportsDel(Integer sId){
        try {
            sportsCircleService.dellSports(sId);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    //根据标题帖子
    @GetMapping(value = "SportNameFindAll")
    public List<Sportscircle> SportNameFindAll(String sName){
        List<Sportscircle> list =  sportsCircleService.nameFindAll(sName);
        return list;
    }

    //根据标签查询贴子
    @GetMapping(value = "SportStypeFindAll")
    public List<Sportscircle> StypeCircleFindAll(String sType){
        List<Sportscircle> list =  sportsCircleService.stypeFindAll(sType);
        return list;
    }

    //查询专区
    @GetMapping(value = "ZhuanQuFindAll")
    public List<Zhuanqu> ZhuanQuFindAll(){
        List<Zhuanqu> list =  sportsCircleService.ZhuanQuFindAll();
        return list;
    }


    //添加专区
    @GetMapping(value = "zhuanquAdd")
    public Integer zhuanquAdd(String zname,Integer indexnum,Zhuanqu zhuanqu){
        zhuanqu.setZname(zname);
        zhuanqu.setIndexnum(indexnum);
        try {
            sportsCircleService.zqAdd(zhuanqu);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    //删除专区
    @GetMapping(value = "zhuanquDel")
    public Integer zhuanquDel(Integer id){
        try {
            sportsCircleService.zaDel(id);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    //推荐专区
    @GetMapping(value = "tuijianzq")
    public Integer tuijianzq(Integer id){
        try {
            sportsCircleService.tuijianzq(id);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    //降级专区
    @GetMapping(value = "jiangjizq")
    public Integer jiangjizq(Integer id){
        try {
            sportsCircleService.jiangjizq(id);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    //查询专区
    @GetMapping(value = "ZhuanQuIndexNumFindAll")
    public List<Zhuanqu> ZhuanQuIndexNumFindAll(){
        List<Zhuanqu> list =  sportsCircleService.ZhuanQuIndexNumFindAll();
        return list;
    }


}
